#include "MenuSystem.h"
#include "IrControl.h"
#include <fcntl.h>
#include "Menu/Tool/frame.h"
#include "BF533/TouchPanel.h"
#include "BF533/FreadWrite.h"

#define ITEM_VIDEO_TPADJ	(0)
#define ITEM_VIDEO_DISPADJ	(1)
#define ITEM_VIDEO_DISPDEF	(2)
#define ITEM_VIDEO_EXIT		(3)

#define MENU_VIDEO_BUTTON_QTY   (4)


TPosCoor	MenuVideoItemPos[MENU_VIDEO_BUTTON_QTY] = { {28,164}, {252,164}, {477,164},{577,376} };
TRect		MenuVideoTouchRect[MENU_VIDEO_BUTTON_QTY];
static 		TPosCoor	Touch_clbr_pos[5];
int		MenuVideoFocus = -1;
TFrame		MenuVideoFrame;
char		*MenuVideoInfoStr[MENU_VIDEO_BUTTON_QTY] = {"TP ADJ","DISP ADJ","DEF","EXIT"};
extern	unsigned char read_info_data[10];
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;


void MenuVideoDrawBackgroup(TCanvas *pCanvas)
{
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "VIDEO SET" );

}

void MenuVideoDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "VIDEO SET" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuVideoDrawVolume(MenuVideoFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void MenuVideoDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= MENU_VIDEO_BUTTON_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_MENU_VIDEO_MAIN_ITEM_FOCUS_TPADJ+idx ;
	}else{
		GetJpegIdx = IMG_MENU_VIDEO_MAIN_ITEM_TPADJ+idx ;
	}
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	pos = MenuVideoItemPos[idx];
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_VIDEO_SET_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	MenuVideoTouchRect[idx].left = pos.x;
	MenuVideoTouchRect[idx].right = pos.x + pJpeg.Width;
	MenuVideoTouchRect[idx].top = pos.y;
	MenuVideoTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, MenuVideoTouchRect[idx], TEXT_ALIGN_CENTER, MenuVideoInfoStr[idx] );
	
}

void MenuVideoDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		MenuVideoDrawBackgroup(MenuVideoFrame.pCanvas);
	for( i = 0; i < MENU_VIDEO_BUTTON_QTY; i++ )
		MenuVideoDrawItem( MenuVideoFrame.pCanvas, i, FALSE );
	if (mute_status == 1)
		{
			MenuVideoDrawVolume(MenuVideoFrame.pCanvas,1);
		}
		
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}


int MenuVideoGetTouchID( TPosCoor pos )
{
	int i;		
	
		for( i = 0; i < MENU_VIDEO_BUTTON_QTY; i++ )
			if( ut_PtInRect( pos, MenuVideoTouchRect[i] ) )
				{				
				return i;
				}
		
	return -1;
}

BOOL MenuVideoBtnEnter(int idx)
{
	switch(idx)
	{
		case ITEM_VIDEO_TPADJ:
			TPAdjInit();
			break;
		case ITEM_VIDEO_DISPADJ:
			VideoDispInit();
			break;
		case ITEM_VIDEO_DISPDEF:
			VideoDispDefInit();
			break;	
		case ITEM_VIDEO_EXIT:
			MenuMainInit();
			break;
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}


void MenuVideoInit()
{
	ir_SetCurrentID( IR_MENU_VIDEO_INIT );
	frm_Create( &MenuVideoFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	MenuVideoDraw( TRUE );
	printf ("in MenuVideoInit ~~\n");
	
}
/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:	
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;											
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MenuVideoDrawVolume(MenuVideoFrame.pCanvas,1);
									break;
								}
								break;
						
					case	ID_KEYPAD:	
						switch (read_info_data[3])
							{
									case 	ID_MUTE:
										MenuVideoDrawVolume(MenuVideoFrame.pCanvas,1);
										break;
									case	ID_DMUTE:	
										MenuVideoDrawVolume(MenuVideoFrame.pCanvas,3);
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
										
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
										
									case 	ID_FMAM:
										MenuRadioInit();
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();								
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;
					case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;	
						
					default:	
							rc = -1;						
						}	
	return 0;
}


void MenuVideoInput(UInt32 key)
{
		int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( MenuVideoFocus = MenuVideoGetTouchID( pos )) != -1 )
				{
					MenuVideoDrawItem( MenuVideoFrame.pCanvas, MenuVideoFocus, TRUE );
					dt_Present_part(MenuVideoTouchRect [MenuVideoFocus]);
				}
			}
		}
		else
		{
			if( ( focus = MenuVideoGetTouchID( pos ) ) == MenuVideoFocus )
				MenuVideoBtnEnter( MenuVideoFocus );
			else if( MenuVideoFocus != -1 )
			{
				MenuVideoDrawItem( MenuVideoFrame.pCanvas, MenuVideoFocus, FALSE );
				dt_Present_part(MenuVideoTouchRect [MenuVideoFocus]);
			}
			MenuVideoFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}


